package com.cskaoyan.config2;

import com.alibaba.druid.pool.DruidDataSource;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import javax.sql.DataSource;

/**
 * @Value
 * @author stone
 * @date 2022/10/10 16:10
 */
@Configuration
public class DataSourceConfiguration2 {

    // Magic Value 魔法值
    /*String driverClassName = "com.mysql.jdbc.Driver";
    String url = "jdbc:mysql://localhost:3306/cskaoyan_db?useUnicode=true&characterEncoding=utf-8";
    String username = "root";
    String password = "123456";*/

    @Value("${cskaoyan.datasource.driverClassName}")
    String driverClassName;    //  = "com.mysql.jdbc.Driver";
    @Value("${cskaoyan.datasource.url}")
    String url;    //  = "jdbc:mysql://localhost:3306/cskaoyan_db?useUnicode=true&characterEncoding=utf-8";
    @Value("${cskaoyan.datasource.username}")
    String username;    //  = "root";
    @Value("${cskaoyan.datasource.password}")
    String password;    //  = "123456";

    @Bean
    public DataSource dataSource2() {
        DruidDataSource dataSource = new DruidDataSource();
        // ctrl + alt + v
        dataSource.setDriverClassName(driverClassName);
        dataSource.setUrl(url);
        dataSource.setUsername(username);
        dataSource.setPassword(password);
        return dataSource;
    }
}
